522A - Reposts - CodeForces Solution


*special problem dfs and similar dp graphs trees *1200

Please click on ads to support us..

Python Code:

class Solution:

	def solve(self, reposts, n):
		lookup = {'polycarp': 1}

		for repost in reposts:

			name1, _, name2 = repost.split(" ")

			lookup[name1] = 1 + lookup[name2]

		print(max(lookup.values()))
		return

def main():
	
		t = 1

	sol = Solution()

	while t:
		n = int(input())
		
		reposts = []
		for _ in range(n):
			reposts.append(input().lower())

		sol.solve(reposts, n)

		t -= 1

if __name__ == '__main__':

	main()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define pb push_back
#define F 1000000007
#define S second
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vs;

void dfs(map<string,vector<string>> &adj,string &node,unordered_map<string,bool>&visited, unordered_map<string,int> &height){
    /*
       Take action on vertex after entering
        the vertex 
        */   
  
  

    for(auto i:adj[node]){
       /*
       Take action on child before entering
        the child 
        */

        //  cout<<i<<endl;
    //    cout<<"par:"<<node<<" child:"<<i<<endl;
  
         dfs(adj,i,visited,height);
         
          /*
       Take action on child after exiting
        the child
        */ 
       
        height[node] = max(height[i]+1,height[node]);
       
    }
      /*
       Take action on vertex before exiting
        the vertex 
        */ 
    //    return;
}
int main(){
    int n,m;
    cin>>n;
    map<string,vector<string>> adj;
    string root;
    for (int i = 1; i <=n ; i++)
    {
        string s; string u,v;
       cin>>u;
      
        cin>>s;
        cin>>v;
        transform(v.begin(), v.end(), v.begin(), ::tolower);
        transform(u.begin(), u.end(), u.begin(), ::tolower);
        if(i==1){
            root = v;
        }

    //   int k=0;
    //   while(s[k]!=' '){
    //     u.push_back(s[k]);
    //     k++;
    //   }
    //   k++;
    //   while(s[k]!=' '){
    //     k++;
    //   }
    //   while(k<s.size()){
    //     v.push_back(s[k]);
    //     k++;
    //   }
    
    //    cout<<u<<" "<<v<<endl;
      adj[v].push_back(u);

    
    }
   
    
//  for(auto i:adj){
//     cout<<i.first<<" -> ";
//    for(auto j:i.second){
//     cout<<j<<" ";
//    }
//    cout<<endl;
    
//  }
    
    unordered_map<string,bool> visited;
   unordered_map<string,int> height;
      for(auto i:adj){
    visited[i.first]=0;
    height[i.first]=0;
   }
   
     int tree = 0;
    dfs(adj,root,visited,height);
    cout<<height[root]+1<<endl;

  
    
  
    
    

    
}


Comments

Submit
0 Comments
More Questions

Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time